package com.csi.controller.access;
import com.csi.domain.UserAssess;

import com.csi.service.AssessService;
import com.csi.utils.JsonResult;
import com.csi.utils.JwtUtil;
import com.csi.utils.ReturnResults;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@RequestMapping("/assess")
@RestController
@Tag(name="Assess",description = "评价相关接口")
public class AssessController {

    @Autowired
    private AssessService assessService;

    /**
     * 新增评价
     * @param userAssess
     * @param request
     * @return
     */
    @PostMapping("/save")
    @Operation(summary = "用户评论",description = "用户评价酒店")
    public Object save(@RequestBody UserAssess userAssess, HttpServletRequest request){
        String token =  request.getHeader("token");
        long userId = JwtUtil.getTokenBody(token).get("userId", Long.class);
        userAssess.setUserId(userId);
        userAssess.setAssessDate(new Timestamp(System.currentTimeMillis()));
        int saved = assessService.save(userAssess);
        // 返回结果
        if (saved > 0) {
            return new JsonResult(ReturnResults.ACCESS_SUCCESS,  "评论成功！");
        } else {
            return new JsonResult(ReturnResults.ACCESS_FAILED, "评论失败！");
        }
    }
    @GetMapping("/find")
    @Operation(summary = "酒店评价",description = "查看酒店对应的评价")
    public Object find(@RequestParam("hotelId") long hotelId){
        List<UserAssess> assessList = assessService.find(hotelId);
        if (assessList != null){
            return new JsonResult<>(ReturnResults.ACCESS_SUCCESS,assessList,"查找成功");
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"该酒店无评价");
    }


}
